//
//  LSZVideoPlaybackViewController+LCMorePicturesTable.m
//  BaiduAd_Example
//
//  Created by admin on 2018/7/16.
//  Copyright © 2018年 iOS11luchao. All rights reserved.
//

#import "LSZVideoPlaybackViewController+LCMorePicturesTable.h"
#import "BaiduMobAdSDK/BaiduMobAdNativeAdView.h"
#import "BaiduMobAdSDK/BaiduMobAdNativeAdDelegate.h"
#import "BaiduMobAdSDK/BaiduMobAdNative.h"
#import "BaiduMobAdSDK/BaiduMobAdNativeAdObject.h"
#import "BaiduMobAdSDK/BaiduMobAdNativeVideoView.h"
#import "BaiduMobAdSDK/BaiduMobAdNativeWebView.h"

#define ADID_TYPE1 @"5864842"
//@"5864786"
//4393166" //组图
#define PublisherId  @"b3a6d662"
//@"ccb60059" //应用id

@implementation LSZVideoPlaybackViewController (LCMorePicturesTable)

#pragma mark - 广告相关设置
///请求广告
- (void)pressToLoadAd
{
    self.adViewArray = [[NSMutableArray alloc] initWithCapacity:0];
    //#warning ATS默认开启状态, 可根据需要关闭App Transport Security Settings，设置关闭BaiduMobAdSetting的supportHttps，以请求http广告，多个产品只需要设置一次.    [BaiduMobAdSetting sharedInstance].supportHttps = NO;
    
    if (!self.native)
    {
        self.native = [[BaiduMobAdNative alloc]init];
        self.native.delegate = self;
    }
    //请求原生广告
    [self.native requestNativeAds];
}


//创建后获得的应用id
- (NSString*)publisherId
{
    return PublisherId;
}

//创建后获得的信息流广告位id
-(NSString*)apId
{
    return ADID_TYPE1; //组图
}

//广告返回成功
//成功返回广告字段，BaiduMobAdNativeAdObject array
- (void)nativeAdObjectsSuccessLoad:(NSArray*)nativeAds{
    
    self.adViewArray = [NSMutableArray array];
    self.adsArray = [nativeAds copy];
    for(int i = 0; i < [nativeAds count]; i++){
        BaiduMobAdNativeAdObject *object = [nativeAds objectAtIndex:i];
        BaiduMobAdNativeAdView *view = [self createNativeAdViewWithframe:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, LCPicsHeight) object:object];
        //传入用来展示广告详情页的viewcontroller (也可以不传)
        view.presentAdViewController = self;
        [self.adViewArray addObject:view];
        
//        UIView *bgView = [[UIView alloc] init];
//        bgView.frame =
//        CGRectMake(0, 0, view.width, view.height/2);
//        bgView.backgroundColor = arc4random_color;//调用随机色
//        bgView.alpha = 0.5;
//        [view addSubview:bgView];
        
        // 展现前检查是否过期，30分钟广告将过期，如果广告过期，请放弃展示并重新请求
        if ([object isExpired]) {
            continue;
        }
        
        //如果开启了多图模式，则可以在标准的BaiduMobAdNativeAdView上添加自定义多图
        if([object.morepics count]>0 ){
            NSLog(@"多图url array 为 %@",object.morepics);
            UIImageView *imageview = [[UIImageView alloc] init] ;
            [view addSubview:imageview];
        }
        
        __block LSZVideoPlaybackViewController *weakSelf = self;
        // 加载和显示广告内容
        [view loadAndDisplayNativeAdWithObject:object completion:^(NSArray *errors) {
            if (!errors) {
                // 确定视图显示在window上之后再调用trackImpression，不要太早调用
                //在tableview或scrollview中使用时尤其要注意

//                [weakSelf.tableViewList reloadData];
                [weakSelf.tableViewList reloadData];
            }
            
        }];
    }
}

//广告返回失败
-(void)nativeAdsFailLoad:(BaiduMobFailReason)reason
{
    NSLog(@"nativeAdsFailLoad,reason = %d",reason);
    self.adViewArray = [NSMutableArray array];
    [self.tableViewList reloadData];
}

//广告被点击，打开后续详情页面
-(void)nativeAdClicked:(UIView *)nativeAdView
{
    NSLog(@"nativeAdClicked");
}

//广告详情页被关闭
-(void)didDismissLandingPage:(UIView *)nativeAdView
{
    NSLog(@"didDismissLandingPage");
}

#pragma mark - adConfig
- (BaiduMobAdNativeAdView *)createNativeAdViewWithframe:(CGRect)frame object:(BaiduMobAdNativeAdObject *)object
{
    BaiduMobAdNativeAdView *nativeAdView;
    
    UILabel *adLogoLabel = [[UILabel alloc] initWithFrame:CGRectMake(286, 318, 24, 12)];

    if (object.materialType == HTML) {
        //#warning 信息流模版广告
        //#warning 模板广告内部已添加百度广告logo和熊掌，开发者无需添加
        BaiduMobAdNativeWebView *webview = [[BaiduMobAdNativeWebView alloc] initWithFrame:frame andObject:object];
        nativeAdView = [[BaiduMobAdNativeAdView alloc] initWithFrame:frame
                                                             webview:webview];
        [nativeAdView addSubview:adLogoLabel];
        adLogoLabel.font = FONT_12;
        adLogoLabel.text = @"百度广告";
        adLogoLabel.textColor = RGBColor(166, 166, 166);
        [adLogoLabel sizeToFit];
        [adLogoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(nativeAdView.mas_right).offset(-15);
            make.width.and.height.lessThanOrEqualTo(adLogoLabel);
            make.top.equalTo(nativeAdView.mas_top);
        }];
        
        UIView *adLogoLabelBGBiew = [[UIView alloc] init];
        [adLogoLabel addSubview:adLogoLabelBGBiew];
        
        adLogoLabelBGBiew.layer.masksToBounds = YES;
        adLogoLabelBGBiew.layer.cornerRadius = 9.0 ;
        adLogoLabelBGBiew.layer.borderColor = RGBColor(166, 166, 166).CGColor;//设置边框颜色
        adLogoLabelBGBiew.layer.borderWidth = 1.0f;//设置边框颜色
        
        [adLogoLabelBGBiew mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(adLogoLabel.mas_left).offset(-5);
            make.top.equalTo(adLogoLabel.mas_top).offset(-2.5);
            make.right.equalTo(adLogoLabel.mas_right).offset(5);
            make.bottom.equalTo(adLogoLabel.mas_bottom).offset(2.5);
        }];

    }
    
    nativeAdView.backgroundColor = [UIColor whiteColor];
    return nativeAdView;
}

- (void)sendVisibleImpressionAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *visiblePath = [self.tableViewList indexPathsForVisibleRows];
    if ([visiblePath containsObject:indexPath]) {
        if ([self.adViewArray count]> indexPath.row) {
            // 确定视图显示在window上之后再调用trackImpression，不要太早调用
            //在tableview或scrollview中使用时尤其要注意
            BaiduMobAdNativeAdView *view = [self.adViewArray objectAtIndex:indexPath.row];
            BaiduMobAdNativeAdObject *object = [self.adsArray objectAtIndex:indexPath.row];
            [object trackImpression:view];
        }
    }
}

/**
 * 大图高度，仅用于信息流模版广告
 */
-(NSNumber*)baiduMobAdsHeight {
    return [NSNumber numberWithFloat:(self.view.frame.size.width-20) / 4 * 3];//180
}

/**
 * 大图宽度，仅用于信息流模版广告 3/4     320
 */
-(NSNumber*)baiduMobAdsWidth {
    return [NSNumber numberWithFloat:self.view.frame.size.width-20];
}

- (BOOL)enableLocation {
    return NO;
}

- (void)baiduAdDealloc {
    self.native.delegate = nil;
    self.native = nil;
    for (UIView *adview in self.adViewArray) {
        [adview removeFromSuperview];
    }
    [self.adViewArray removeAllObjects];
    self.adViewArray = nil;
    self.adsArray = nil;
}


@end
